package net.edu_soft.ec.common.interceptor;

import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.exception.PermissionException;

import javax.servlet.http.HttpServletRequest;


/**
 * @Description: 自定义异常拦截器
 * @author puing
 * @date 2019年12月6日
 */
//@RestControllerAdvice
public class ExceptionInterceptor {

	/**
	 * @Title: operationException
	 * @Description: 操作异常处理
	 * @param e
	 * @return
	 */
    @ExceptionHandler(OperationException.class)
    public ResultVo operationException(OperationException e){
        return ResultVo.failed(400, e.getMessage(),null);
     }

    /**
     * 
     * @Title: parameterException
     * @Description: 参数异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(ParameterException.class)
    public ResultVo parameterException(ParameterException e){
        return ResultVo.failed(400, e.getMessage(),null);
     }
    
    /**
     * 
     * @Title: permissionException
     * @Description: 权限异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(PermissionException.class)
    public ResultVo permissionException(PermissionException e){
        return ResultVo.failed(400, e.getMessage(),null);
     }

	@ExceptionHandler(UnauthorizedException.class)
	public String defaultExceptionHandler(HttpServletRequest req, Exception e){
		return "对不起，你没有访问权限！";
	}

	@ExceptionHandler(NullPointerException.class)
	public ResultVo nullPointerException(PermissionException e){
		return ResultVo.failed(500, e.getMessage(),null);
	}
    
    /**
	 * 	拦截所有运行时的全局异常   
	 */
	@ExceptionHandler(RuntimeException.class)
	public ResultVo runtimeException(RuntimeException e) {
		return ResultVo.failed(500, e.getMessage(),null);
	}
 
	/**
	 * 	系统异常捕获处理
	 */
	@ExceptionHandler(Exception.class)
	public ResultVo exception(Exception e) {
		return ResultVo.failed(501, e.getMessage(),null);
	}
}
